package com.iflytek.springboot_medicine.controller;

import com.iflytek.springboot_medicine.entity.Income;
import com.iflytek.springboot_medicine.entity.Medicine;
import com.iflytek.springboot_medicine.pojo.IncomePojo;
import com.iflytek.springboot_medicine.pojo.MedicinePojo;
import com.iflytek.springboot_medicine.pojo.ResponseMessage;
import com.iflytek.springboot_medicine.service.impl.IncomeServiceImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/income")
public class IncomeController {
    private final IncomeServiceImpl incomeServiceImpl;

    public IncomeController(IncomeServiceImpl incomeServiceImpl) {
        this.incomeServiceImpl = incomeServiceImpl;
    }

    @GetMapping("/findByCategoryAndDateRange")
    public ResponseMessage<Page<IncomePojo>> findByCategoryAndDateRange(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        Pageable pageable = incomeServiceImpl.findIncome(pageNum, pageSize);
        Page<IncomePojo> incomes = incomeServiceImpl.findStockInsByCategoryAndDateRange(name, startDate, endDate, pageable);
        return ResponseMessage.success(incomes);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @RequestMapping("/delIncomeById/{id}")
    public ResponseMessage deleteIncome(@PathVariable int id) {
        if (id > 0){
            incomeServiceImpl.deleteIncomeById(id);
        }
        return ResponseMessage.success();

    }

    /**
     * 批量删除
     */
    @RequestMapping("/deleteBatchType")
    public ResponseMessage getUserById(@RequestBody List<Integer> ids) {
        if (ids == null || ids.size() == 0) {
            return ResponseMessage.error("删除失败");
        }
        incomeServiceImpl.deleteAllInBatch(ids);
        return ResponseMessage.success();
    }

    /**
     * 更新药材信息
     * @param incomePojo
     * @return
     */
    @RequestMapping("/updateIncome")
    public ResponseMessage<IncomePojo> updateMedicine(@RequestBody IncomePojo incomePojo) {
        if (incomePojo == null) {
            return ResponseMessage.error("更新失败");
        }
        String category = incomePojo.getCategory();
        int i = incomeServiceImpl.selectByName(category);
        Income income = new Income(incomePojo.getId(),i,incomePojo.getName(),incomePojo.getAccount(),incomePojo.getIncome_user(),incomePojo.getIncome_date());
        incomeServiceImpl.saveIncome(income);
        return ResponseMessage.success(incomePojo);
    }

    /**
     * 增加药材信息
     * @param incomePojo
     * @return
     */
    @RequestMapping("/addIncome")
    public ResponseMessage<IncomePojo> addIncome(@RequestBody IncomePojo incomePojo) {
        if (incomePojo == null) {
            return ResponseMessage.error("添加失败");
        }
        String category = incomePojo.getCategory();
        int i = incomeServiceImpl.selectByName(category);
        Income income = new Income(incomePojo.getId(),i,incomePojo.getName(),incomePojo.getAccount(),incomePojo.getIncome_user(),incomePojo.getIncome_date());
        incomeServiceImpl.saveIncome(income);
        return ResponseMessage.success(incomePojo);
    }

}
